Yii2.0 | 您所在的位置:网站首页 › java csv文件读取 › Yii2.0 |
Form model First of all, you need to create a model that will handle file upload. Create models/UploadForm.php with the following content: namespace app\models;use yii\base\Model;use yii\web\UploadedFile;/** * UploadForm is the model behind the upload form. */class UploadForm extends Model{ /** * @var UploadedFile|Null file attribute */ public $file; /** * @return array the validation rules. */ public function rules() { return [ [['file'], 'file'], ]; }}In the code above, we created a model UploadForm with an attribute $file that will become in the HTML form. The attribute has the validation rule named file that uses FileValidator. Form viewNext create a view that will render the form. SubmitThe 'enctype' => 'multipart/form-data' is important since it allows file uploads. fileInput() represents a form input field. ControllerNow create the controller that connects form and model together: namespace app\controllers;use Yii;use yii\web\Controller;use app\models\UploadForm;use yii\web\UploadedFile;class SiteController extends Controller{ public function actionUpload() { $model = new UploadForm(); if (Yii::$app->request->isPost) { $model->file = UploadedFile::getInstance($model, 'file'); if ($model->validate()) { $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension); } } return $this->render('upload', ['model' => $model]); }}Instead of model->load(...) we are using UploadedFile::getInstance(...). UploadedFile does not run the model validation. It only provides information about the uploaded file. Therefore, you need to run validation manually via $model->validate(). This triggers theFileValidator that expects a file: $file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE //in code frameworkIf validation is successful, then we're saving the file: $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);If you're using "basic" application template then folder uploads should be created under web. That's it. Load the page and try uploading. Uplaods should end up in basic/web/uploads. Additional informationRequired ruleIf you need to make file upload mandatory use skipOnEmpty like the following: public function rules(){ return [ [['file'], 'file', 'skipOnEmpty' => false], ];}MIME typeIt is wise to validate type of the file uploaded. FileValidator has property $extensions for the purpose: public function rules(){ return [ [['file'], 'file', 'extensions' => 'gif, jpg',], ];}The thing is that it validates only file extension and not the file content. In order to validate content as well use mimeTypes property ofFileValidator: public function rules(){ return [ [['file'], 'file', 'extensions' => 'jpg, png', 'mimeTypes' => 'image/jpeg, image/png',], ];}List of common media types Validating uploaded imageIf you upload an image, ImageValidator may come in handy. It verifies if an attribute received a valid image that can be then either saved or processed using Imagine Extension. Uploading multiple filesIf you need download multiple files at once some adjustments are required. View: SubmitThe difference is the following line: Controller: namespace app\controllers;use Yii;use yii\web\Controller;use app\models\UploadForm;use yii\web\UploadedFile;class SiteController extends Controller{ public function actionUpload() { $model = new UploadForm(); if (Yii::$app->request->isPost) { $files = UploadedFile::getInstances($model, 'file'); foreach ($files as $file) { $_model = new UploadForm(); $_model->file = $file; if ($_model->validate()) { $_model->file->saveAs('uploads/' . $_model->file->baseName . '.' . $_model->file->extension); } else { foreach ($_model->getErrors('file') as $error) { $model->addError('file', $error); } } } if ($model->hasErrors('file')){ $model->addError( 'file', count($model->getErrors('file')) . ' of ' . count($files) . ' files not uploaded' ); } } return $this->render('upload', ['model' => $model]); }}The difference is UploadedFile::getInstances($model, 'file'); instead of UploadedFile::getInstance($model, 'file');. Former returns instances for all uploaded files while the latter gives you only a single instance. 》》第三方上传组件参考 1.http://blog.csdn.net/zbc496218/article/details/51442051 (http://demos.krajee.com/widget-details/fileinput#settings) 0 0 Yii2.0-文件上传操作类-UploadedFile yii2的文件土拍你上传类UploadedFile的使用 Yii2文件上传UploadedFile,单文件和多文件上传及验证示例Debug-OK yii2利用自带UploadedFile上传图片 Yii2.0文件上传 yii2.0 文件上传 yii2.0表单上传文件 yii2.0多文件上传 yii2.0实现文件上传 Yii2.0-单文件上传 Yii2.0-多文件上传 yii2利用自带UploadedFile上传图片_so easy [ 2.0 版本 ] Yii2上传文件 yii2文件上传 yii2的文件上传 yii2文件上传 yii2文件上传 yii2 文件上传 struts2 下进行文件的上传下载 自旋锁 消除#1366 - Incorrect integer value: '' for column apache安装及配置 使用Flex生成可线程重入的JSON词法分析器 Yii2.0-文件上传操作类-UploadedFile 【JZOJ 4624】字符串匹配 过程及程序块的技巧 前淘宝技术专家谈12306:这个网站很神奇 python 如何优雅地退出子进程 如何在linux和windows下使用版本管理工具git 详解 2016.06.11下午【2016新初一 】普及组模拟题解 Cookie登录 NodeJS学习系列课程笔记(NodeJs简介) |
CopyRight 2018-2019 实验室设备网 版权所有 |